Remote texting demo

Start a Mosquitto container first. For example:

  • Use codes\_demo\1_start_broker.sh to start a Mosquitto container on Raspberry Pi.
  • Config files are in mqtt_config\mqtt.
  • set allow_anonymous true in mqtt_config\mqtt\config\mosquitto.conf to allow anonymous client.

Getting Started

What this notebook does:

  • Using a client on PC
  • List connected nodes
  • Send text to remote node and display on OLED screen.

In [ ]:
import os
import sys
import time
 
sys.path.append(os.path.abspath(os.path.join(os.path.pardir, '..\\codes', 'client')))
sys.path.append(os.path.abspath(os.path.join(os.path.pardir, '..\\codes', 'node')))
sys.path.append(os.path.abspath(os.path.join(os.path.pardir, '..\\codes', 'shared')))
sys.path.append(os.path.abspath(os.path.join(os.path.pardir, '..\\codes', 'micropython')))
 
import client
from collections import OrderedDict

Start client


In [ ]:
the_client = client.Client()
the_client.start()

while not the_client.status['Is connected']:            
    time.sleep(1)
    print('Node not ready yet.')

Prepare messages


In [ ]:
# messages _____________________________________________
messages = OrderedDict()

messages['show text'] = {'message_type': 'command',
                         'command': 'show text',
                         'kwargs': {'text': 'Hello World!', 
                                    'x': 0, 'y': 0, 
                                    'clear_first': True, 
                                    'show_now': True,
                                    'hold_seconds': 5}}

Send out messages and get asynchonous results


In [ ]:
print('\n[______________ Sending messages ______________]\n')

remote_node = 'NodeMCU_8a00'

# send out the messages
for message in messages.values():
    time.sleep(0.1)  # PyCharm needs this delay.
    the_client.request(remote_node, message)

Stop the demo


In [ ]:
# Stopping
the_client.stop()
the_client = None
print('\n[________________ Demo stopped ________________]\n')

In [ ]: